var gdoc = { 
    get_login_status : function() { 
        dojo.byId( 'login_content' ).innerHTML = '<img src="/common/img/ajax-loader.gif" />';
        dojo.xhrGet( { 
            url:               '/app/login/status',
            handleAs:          'json',
            timeout:           5000,
            load:              function( response, ioArgs ) { 
                dojo.byId( 'login_content' ).innerHTML = 
                  response.logged_in ? "You are logged in as " + response.user.username 
                                     : "Not logged in";
                return response;
            },
            error:             function( response, ioArgs ) { 
                dojo.byId( 'login_content' ).innerHTML = '<span class="error">Error</span>';
                return response;
            }
        } );
    },

    get_side_content : function() { 
        dojo.xhrGet( { 
            url:               '/sidebar',
            handleAs:          'text',
            timeout:           5000,
            load:              function( response, ioArgs ) { 
                dojo.byId( 'sidebar' ).innerHTML = response;
                return response;
            },
            error:             function( response, ioArgs ) { 
                dojo.byId( 'side_content' ).innerHTML = '<span class="error">Error</span>';
            }
        } );
    },

    add_list_dividers : function() { 
        var uls = dojo.query( 'ul.taxa_list' );
        uls.forEach( function( node, index ) { 
                var lis = dojo.query( 'li', node );
                lis.forEach( function( linode, liindex ) { 
                        if ( liindex > 0 ) linode.innerHTML = ' | ' + linode.innerHTML;
                    }
                )
            }
        )
    },

    add_crumb_dividers : function() { 
        var lis = dojo.query( 'ul.crumb_list li' );
        lis.forEach( function( node, index ) { 
                if ( index > 0 ) node.innerHTML = ' &gt; ' + node.innerHTML;
            }
        )
    },

    handle_footnotes : function() { 
        var notes = dojo.query( 'span.footnote' );
        var list  = dojo.byId( 'notes_list' );
        if( !list ) return;
        
        notes.forEach( function( node, index ) { 
                var num = index + 1;
                var li = document.createElement( 'li' );
                li.innerHTML = '<a name="note_' + num + '">' + node.innerHTML + '</a>';
                list.appendChild( li );
                node.innerHTML = '<sup><a href="#note_' + num + '">[' + num + ']</a></sup>';
                node.style.display = 'inline';
            }
        )
    }   
};


dojo.addOnLoad( gdoc.get_side_content );
dojo.addOnLoad( gdoc.add_list_dividers );
dojo.addOnLoad( gdoc.add_crumb_dividers );
dojo.addOnLoad( gdoc.handle_footnotes );